package com.mvp.library.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.mvp.library.factory.PresenterMvpFactory;
import com.mvp.library.factory.PresenterMvpFactoryImpl;
import com.mvp.library.presenter.BaseMvpPresenter;
import com.mvp.library.proxy.BaseMvpProxy;
import com.mvp.library.proxy.PresenterProxyInterface;

/**
 * @author 刘镓旗
 */
public class AbstractFragment< V extends BaseMvpView, P extends BaseMvpPresenter< V > > extends Fragment implements PresenterProxyInterface< V, P >
{
	
	/**
	 * 调用onSaveInstanceState时存入Bundle的key
	 */
	private static final String PRESENTER_SAVE_KEY = "presenter_save_key";
	/**
	 * 创建被代理对象,传入默认Presenter的工厂
	 */
	private BaseMvpProxy< V, P > mProxy = new BaseMvpProxy<>( PresenterMvpFactoryImpl.< V, P > createFactory( getClass() ) );
	
	@Override
	public void onCreate( @Nullable Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		if( savedInstanceState != null )
		{
			mProxy.onRestoreInstanceState( savedInstanceState );
		}
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		mProxy.onResume( ( V )this );
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		mProxy.onDestroy();
	}
	
	@Override
	public void onSaveInstanceState( Bundle outState )
	{
		super.onSaveInstanceState( outState );
		outState.putBundle( PRESENTER_SAVE_KEY, mProxy.onSaveInstanceState() );
	}
	
	/**
	 * 可以实现自己PresenterMvpFactory工厂
	 * @param presenterFactory PresenterFactory类型
	 */
	@Override
	public void setPresenterFactory( PresenterMvpFactory< V, P > presenterFactory )
	{
		mProxy.setPresenterFactory( presenterFactory );
	}
	
	/**
	 * 获取创建Presenter的工厂
	 * @return PresenterMvpFactory类型
	 */
	@Override
	public PresenterMvpFactory< V, P > getPresenterFactory()
	{
		return mProxy.getPresenterFactory();
	}
	
	/**
	 * 获取Presenter
	 * @return P
	 */
	@Override
	public P getMvpPresenter()
	{
		return mProxy.getMvpPresenter();
	}
}
